package com.leonzhangxf.spel.reference;

import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

import java.util.List;

/**
 * Inline Lists
 * <p>
 * {} by itself means an empty list. For performance reasons,
 * if the list is itself entirely composed of fixed literals,
 * a constant list is created to represent the expression
 * (rather than building a new list on each evaluation).
 *
 * @author leonzhangxf
 * @date 20190427
 */
public class InlineLists {

    public static void main(String[] args) {
        SpelParserConfiguration parserConfiguration =
            new SpelParserConfiguration(true, true);
        SpelExpressionParser parser = new SpelExpressionParser(parserConfiguration);
        SimpleEvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

        // evaluates to a Java list containing the four numbers
        List numbers = parser.parseExpression("{1,2,3,4}").getValue(context, List.class);
        System.out.println(numbers);

        List listOfLists = parser.parseExpression("{{'a','b'},{'x','y'}}").getValue(context, List.class);
        System.out.println(listOfLists);
    }
}
